package logic

import (
	"api/pkg"
	"context"
	"srv/srv"
	"strconv"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserupHandlerNameLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserupHandlerNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserupHandlerNameLogic {
	return &UserupHandlerNameLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserupHandlerNameLogic) UserupHandlerName(req *types.UserupRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	up, err := l.svcCtx.Srv.UserUp(l.ctx, &srv.UserUpRequest{
		Tel:    req.Tel,
		SmCode: req.SmCode,
	})
	if err != nil {
		return &types.Response{
			Code: 0,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(up.Id, 10))
	if err != nil {
		return &types.Response{
			Code: 0,
			Msg:  err.Error(),
			Data: up,
		}, nil
	}
	return &types.Response{
		Code: 200,
		Msg:  "用户登录成功",
		Data: handler,
	}, nil
}
